﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using Data;
using Framework;

namespace PictureControls
{
    public class DisplayPicture : WebControl
    {
        
        private string title;
        private string desc;
        private string pictureId;
        private string link;

        #region Properties
        public string Description
        {
            get { return desc; }
            set { desc = value; }
        }

        public string Title
        {
            get { return title; }
            set { title = value; }
        }

        public string PictureId
        {
            get { return pictureId; }
            set { pictureId = value; }
        }

        public string Link
        {
            get { return link; }
            set { link = value; }
        }

        /// <summary>
        /// La clé de tag à écrire
        /// </summary>
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }
        #endregion

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            #region delete button
            ImageButton deleteButt = new ImageButton();
            deleteButt.ImageUrl = "/Images/Test/delete.png";
            //deleteButt.OnClientClick = string.Format("javascript:loadPopup();centerPopup(); return false;");
            #endregion

            #region edit button
            ImageButton editButt = new ImageButton();
            editButt.ImageUrl = "/Images/Test/edit.jpg";
            editButt.OnClientClick = string.Format("javascript:displayUpdatePopup('{0}', '{1}', '{2}'); return false;", PictureId, Title, Description);
            editButt.CssClass = "buttUpdateImg";
            #endregion

            #region Panel to manage buttons
            Panel pnlEditImg = new Panel();
            pnlEditImg.Controls.Add(deleteButt);
            pnlEditImg.Controls.Add(new LiteralControl("<br>"));
            pnlEditImg.Controls.Add(editButt);
            pnlEditImg.CssClass = "float_left";
            #endregion

            UpdateImageDetails updateImagePanel = (UpdateImageDetails)Page.LoadControl("/UserControls/PictureControls/UpdateImageDetails.ascx");
            updateImagePanel.PictureId = PictureId;
            //updateImagePanel.myAjaxBehaviourId = string.Format("animation_{0}", PictureId);
            updateImagePanel.ImgUrl = string.Format("/UserControls/PictureControls/get_picture.ashx?picture_id={0}", PictureId);
            updateImagePanel.ImgDesc = Description;
            updateImagePanel.ImgTitle = Title;
            Controls.Add(updateImagePanel);
        }

        /// <summary>
        /// Chargement du composant
        /// </summary>
        /// <param name="e">Evenement associé au chargement</param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            
        }
        
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
        }
        protected void deleteButt_Click(object sender, EventArgs args)
        {
            ImageData.DeleteImage(MappingTool.CatchIntNull(PictureId));
        }

        protected void editButt_Click(object sender, EventArgs args)
        {
            if (Page.Session[SessionConstants.SessionImageId] != null)
            {
                Page.Session.Contents.Remove(SessionConstants.SessionImageId);
            }
            Page.Session[SessionConstants.SessionImageId] = PictureId;
        }
    }

}
